class PubSub{
    constructor(){
        this.events = {}
    }
    //订阅
    subscribe(event,callback){
        if(this.events[event]){
            //如果有人订阅过了，直接往里面添加
            this.events[event].push(callback)
        }else{
            //没有人订阅过，就新建一个数组再添加进去
            this.events[event] = [callback]
        }
    }
    publish(event,...args){
        const subscribedEvents = this.events[event]
        if(subscribedEvents && subscribedEvents.length){
            subscribedEvents.forEach(callback => {
                callback.call(this,...args)
            })
        }
    }
    unsubscript(event,callback){
        const subscribedEvents = this.events[event]
        if(subscribedEvents && subscribedEvents.length){
            this.events[event] = this.events[event].filter(cb => cb != callback)
        }
    }
}

class Dep{
    constructor(){
        this.sub = []
    }
    addSub(watcher){
        this.sub.push(watcher)
    }
    notify(){
        this.sub.forEach(watcher => {
            watcher.updated()
        })
    }
}
